package com.maoye.ossp.biz.statement.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.gson.Gson;
import com.hotent.base.api.query.QueryFilter;
import com.hotent.base.api.query.QueryOP;
import com.maoye.ossp.biz.brand.entity.Supplier;
import com.maoye.ossp.biz.brand.service.ISupplierService;
import com.maoye.ossp.biz.statement.entity.StatementOrderDetail;
import com.maoye.ossp.biz.statement.entity.StatementSalesSelfrun;
import com.maoye.ossp.biz.statement.service.StatementSalesSelfrunService;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
import com.maoye.ossp.common.web.Validation;
import com.maoye.ossp.sys.org.util.UserUtils;
import com.maoye.ossp.webserviceClient.PosPayWebService;
/**
 * 订单明细controller类
 * @author 梁涛
 *
 */
@Controller
@RequestMapping(value = "/statement/salesSelfrun/")
public class StatementSalesSelfrunController extends BaseController {
	@Resource
	private StatementSalesSelfrunService statementSalesSelfrunService;
	
	@Resource
	private ISupplierService supplierService;
	
	@RequestMapping(value = "list")
	public String list(StatementSalesSelfrun statementSalesSelfrun,
			HttpServletRequest request, Model model) {
		//获取登陆用户Id 
		Long userId=UserUtils.getCurrentUserId();
		//根据当前用户Id 如果是供应商 获取用户获取供应商信息
		Supplier supplier=supplierService.get(userId);
		QueryFilter queryFilter = getQuerFilter(request,statementSalesSelfrun );	
		//设置供应商编码
		if(supplier!=null){
			queryFilter.addFilter("supplierNo", supplier.getSupplierNo(), QueryOP.EQUAL);
		 }
		Page<StatementSalesSelfrun> page = (Page<StatementSalesSelfrun>) statementSalesSelfrunService
				.find(queryFilter);
		/*List<StatementSalesSelfrun> list=page.getList();
		if(list!=null){
		for(StatementSalesSelfrun salesSelfrun:list){
			JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();      
			factory.setServiceClass(PosPayWebService.class);        
			factory.setAddress("http://202.105.115.147:9000/ossp-web/service/pospayWebService");               
			PosPayWebService service = (PosPayWebService) factory.create();              
			String result = service.getGoodsInfo(salesSelfrun.getGoodsbar(),salesSelfrun.getGoodsCode());//商品条码 和商品编码
			Gson gson = new Gson();
			JSONObject a = new JSONObject(result);
			Object b=a.get("data");
			JSONArray array=new JSONArray(b.toString());
			for(int i=0;i<array.length();i++){
				JSONObject json=(JSONObject)array.get(i);
				salesSelfrun.setBarCode(String.valueOf(json.get("goodscode")).substring(10,18));
				salesSelfrun.setGoodsbar(String.valueOf(json.get("barcode")));
				salesSelfrun.setGoodsName(String.valueOf(json.get("goodsname")));	
				salesSelfrun.setGoodsModel(String.valueOf(json.get("goodsModel")));
			}
		    }
		}*/
		model.addAttribute("page", page);
		model.addAttribute("statementSalesSelfrun",statementSalesSelfrun);
		return resultPage("/biz/statement/statementSalesSelfrun/list", model, request);
	}
	
	@RequestMapping(value = "exportExcel")
	public void exportExcel(StatementSalesSelfrun statementSalesSelfrun,
			HttpServletRequest request,HttpServletResponse response) throws IOException{
		//获取登陆用户Id 
		Long userId=UserUtils.getCurrentUserId();
		//根据当前用户Id 如果是供应商 获取用户获取供应商信息
		Supplier supplier=supplierService.get(userId);
		String params = request.getParameter("params");
		Map infoContent = null;
		if (Validation.isNotEmpty(params))
		{
			ObjectMapper mapper = new ObjectMapper();
			infoContent = mapper.readValue(params, Map.class);
			if(supplier!=null){
				infoContent.put("supplierNo",supplier.getSupplierNo());
			 }
		}
		else
		{
			infoContent = new HashMap();
			if(supplier!=null){
				infoContent.put("supplierNo",supplier.getSupplierNo());
			 }
		}
		try {
			// 设置输出的格式
			response.reset();
			response.setContentType("application/vnd.ms-excel");
			//获取浏览器类型
			String user_agent = request.getHeader("User-Agent").toLowerCase();
			String fileName="";
			//为不同的浏览器，对文件名进行不同的编码转换
			if(user_agent.indexOf("firefox") > 0){
				 fileName =  new String("自营销售".getBytes("UTF-8"), "iso8859-1");
			}else{
				fileName = URLEncoder.encode("自营销售", "UTF-8");
			}
			response.setHeader("Content-disposition","attachment; filename="+fileName+".xls");
			OutputStream os = response.getOutputStream();
			statementSalesSelfrunService.doSaveToExcel(infoContent,os);
			response.flushBuffer();
		} catch(IOException ex){
			System.out.println("导出数据出错");
		} catch(Exception e){
			System.out.println(e.getMessage());
		}
	}

	public StatementSalesSelfrunService getStatementSalesSelfrunService() {
		return statementSalesSelfrunService;
	}

	public void setStatementSalesSelfrunService(
			StatementSalesSelfrunService statementSalesSelfrunService) {
		this.statementSalesSelfrunService = statementSalesSelfrunService;
	}

}
